/**
 * create by ck on 2016/8/15.
 */
$(document).ready(function(){
    var schoolId  = xhTool.getQuery('iSchoolId');
    var classNameDom = $('#sClassName');
    var openClassDom = $('#bt_Open_Class');
    var tab = $('#tab_Class');
    //var appCode   = xhTool.getQuery('iAppCode');
    //var teacherId = xhTool.getQuery('iTeacherId');
    //输入班级名称则激活按钮
    classNameDom.on('keyup', function(){
        if($(this).val() != '') {
            openClassDom.setButtonEnable('bs');
        }
        else {
            openClassDom.setButtonDisable('bs');
        }
    }).on('keydown', function(event) {
        if(event.keyCode == 13) {
            openClassDom.click();
        }
    });
    ////增加教师按钮
    //$('#bt_Add_Teacher').click(function(){
    //     window.top.createModal("<iframe src='Class/teacherOutClassList?iSchoolId="+schoolId+"' frameborder='0' style='width: 100%;height:440px;'></iframe>","增加教师","确认增加","lg");
    //});
    //开课按钮
    openClassDom.click(function(){
        var className = classNameDom.val();
        if (tab.tabs('exists', className)){         //判断标签页是否已打开
            tab.tabs('select', className);
            return false;
        }
        $.post(baseUrl + 'Class/addClass', {sClassName: className}, function(classId){
            if(classId == 'fail'){
                window.top.xhTop.showMessage({title: '班级新建失败！', type: 'error'});
                return false;
            }
            var src = baseUrl + 'Class/studentClassList?iSchoolId=' + schoolId + '&iSCLId=' + classId;
            var content = '<iframe name="class_' + classId + '" frameborder="0" style="width:100%;" src="' + src + '"></iframe>';
            tab.tabs('add', {
                title:      className,
                content:    content,
                closable:   true,
                border:     false
            });
        });
        classNameDom.val('');
        openClassDom.setButtonDisable('bs');
    });
    //完成开课按钮
    $('#bt_Confirm_Class').click(function(){
        window.top.xhTop.showModal({
            title: '提示',
            content: '确认开班？',
            button: '确认',
            size: 'sm'
        });
        confirmOpenClass();
    });
    tab.tabs({
        onBeforeClose: function(title, index){
            var target = this;
            $.messager.confirm('提示', '是否要删除' + title + '班级？', function(r){
                if (r){
                    var classId = $('.tabs-panels > div:eq('+index+') iframe').attr('name').replace('class_', '');//获取班级ID
                    $.post('delClass', {iSCLId: classId}, function(result){
                        if(result != 'success'){
                            xhExtra.showMessage({title: '结果', content: '删除失败！'});
                            return false;
                        }
                        var opts = $(target).tabs('options');
                        var bc = opts.onBeforeClose;
                        opts.onBeforeClose = function(){};  // 允许现在关闭
                        $(target).tabs('close',index);
                        opts.onBeforeClose = bc;            // 还原事件函数
                        xhExtra.showMessage({title: '结果', content: '删除成功！'});    //弹窗提示
                    });
                }
            });
            return false;	// 阻止关闭
        }
    });
});

function confirmOpenClass()
{
    $('#bt_modal_confirm', window.top.document).click(function(){
        $(this).unbind('click');
        window.top.xhTop.closeModal();
        $.post(baseUrl + 'Class/completeOpenClass', {}, function(d){
            if(d.indexOf('success') >= 0) {
                window.top.xhTop.showMessage({title: '开班成功！', type: 'success'});
                if(window.parent == window.top){
                    window.top.xhTop.closeTabByName('应用开课');
                }
                else{
                    $('iframe', window.parent.document).attr('src', '');
                }
                return false;
            }
            $('#showResult').html(d);
        });
    });
}